/*****************************************************************************
 * parse.c : parse command line and split into commands and arguments
 *****************************************************************************
 * Copyright (C) 2011 PELinker
 *
 * Authors: Iceberg <iceberg58744@163.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "common.h"
#include "util.h"

#define TAB_CHAR '\t'
#define SPACE_CHAR ' '
#define QUOTE_CHAR '"'
#define HYPHEN_CHAR '-'
#define SLASH_CHAR '/'
#define COLON_CHAR ':'

#ifdef TRACE_ON
#define TRACE PrintInfo
#else
#define TRACE(x)
#endif

#define isBlank(ch) ((TAB_CHAR == (ch)) || (SPACE_CHAR == (ch)))

#define isQuote(ch) (QUOTE_CHAR == (ch))

#define isSwitch(ch) ((HYPHEN_CHAR == (ch)) || (SLASH_CHAR == (ch)))

#define isSeparator(ch) (COLON_CHAR == (ch))

static DWORD SubstractExecuteFilePath(LPCTSTR CommandLine)
{
	DWORD i = 0;

	if (isQuote(CommandLine[i]))
	{
		++i;
		while (CommandLine[i] && !isQuote(CommandLine[i]))
		{
			++i;
		}

		if (isQuote(CommandLine[i]))
		{
			++i;
		}
	}
	else
	{
		while (CommandLine[i] && !isBlank(CommandLine[i]))
		{
			++i;
		}
	}

	while (CommandLine[i] && isBlank(CommandLine[i]))
	{
		++i;
	}

	return i;
}

#define START_STATE 0
#define SWITCH_STATE 1
#define OPTION_STATE 2
#define SEPARATE_STATE 3
#define PARAM_STATE 4
#define FILE_STATE 5

int ParseCommandLine(LPCTSTR CommandLine, Command * const cmd)
{
	DWORD begin, i;

	int isString = 0;

	int CurrentState = START_STATE;

	begin = i = SubstractExecuteFilePath(CommandLine);

	TRACE(" ");
	TRACE(CommandLine + begin);
	TRACE("\n");

	while (CommandLine[i])
	{
		switch (CurrentState)
		{
		case START_STATE:
			TRACE("B");
			if (isBlank(CommandLine[i]))
			{
			}
			else if (isSwitch(CommandLine[i]))
			{
				CurrentState = SWITCH_STATE;
			}
			else
			{
				isString = isQuote(CommandLine[i]);
				if (!isString)
				{
					cmd->files[cmd->TotalFileNumber].ptr = CommandLine + i;
					++(cmd->files[cmd->TotalFileNumber].length);
				}
				CurrentState = FILE_STATE;
			}
			break;
		case SWITCH_STATE:
			TRACE("S");
			if (isBlank(CommandLine[i]) || isQuote(CommandLine[i]) || isSwitch(CommandLine[i]) || isSeparator(CommandLine[i]))
			{
				PrintInfoAux("Invalid option format: ");
				PrintInfoEx(CommandLine + begin, i - begin + 1);
				PrintInfoAux("\n");
				return ERR_INVALID_COMMAND;
			}
			else
			{
				cmd->options[cmd->TotalOptionNumber].name.ptr = CommandLine + i;
				++(cmd->options[cmd->TotalOptionNumber].name.length);
				CurrentState = OPTION_STATE;
			}
			break;
		case OPTION_STATE:
			TRACE("O");
			if (isBlank(CommandLine[i]))
			{
				++(cmd->TotalOptionNumber);
				CurrentState = START_STATE;
			}
			else if (isSeparator(CommandLine[i]))
			{
				CurrentState = SEPARATE_STATE;
			}
			else
			{
				++(cmd->options[cmd->TotalOptionNumber].name.length);
				CurrentState = OPTION_STATE;
			}
			break;
		case SEPARATE_STATE:
			TRACE("C");
			if (isBlank(CommandLine[i]))
			{
				PrintInfoAux("Invalid option format: ");
				PrintInfoEx(CommandLine + begin, i - begin + 1);
				PrintInfoAux("\n");
				return ERR_INVALID_COMMAND;
			}
			else
			{
				isString = isQuote(CommandLine[i]);
				if (!isString)
				{
					cmd->options[cmd->TotalOptionNumber].value.ptr = CommandLine + i;
					++(cmd->options[cmd->TotalOptionNumber].value.length);
				}
				CurrentState = PARAM_STATE;
			}
			break;
		case PARAM_STATE:
			TRACE("P");
			if (isString)
			{
				if (isQuote(CommandLine[i]))
				{
					++(cmd->TotalOptionNumber);
					isString = 0;
					CurrentState = START_STATE;
				}
				else if (!cmd->options[cmd->TotalOptionNumber].value.ptr)
				{
					cmd->options[cmd->TotalOptionNumber].value.ptr = CommandLine + i;
					++(cmd->options[cmd->TotalOptionNumber].value.length);
					CurrentState = PARAM_STATE;
				}
				else
				{
					++(cmd->options[cmd->TotalOptionNumber].value.length);
					CurrentState = PARAM_STATE;
				}
			}
			else
			{
				if (isBlank(CommandLine[i]))
				{
					++(cmd->TotalOptionNumber);
					CurrentState = START_STATE;
				}
				else
				{
					++(cmd->options[cmd->TotalOptionNumber].value.length);
					CurrentState = PARAM_STATE;
				}
			}
			break;
		case FILE_STATE:
			TRACE("F");
			if (isString)
			{
				if (isQuote(CommandLine[i]))
				{
					++(cmd->TotalFileNumber);
					isString = 0;
					CurrentState = START_STATE;
				}
				else if (!cmd->files[cmd->TotalFileNumber].ptr)
				{
					cmd->options[cmd->TotalOptionNumber].name.ptr = CommandLine + i;
					++(cmd->files[cmd->TotalFileNumber].length);
					CurrentState = FILE_STATE;
				}
				else
				{
					++(cmd->files[cmd->TotalFileNumber].length);
					CurrentState = FILE_STATE;
				}
			}
			else
			{
				if (isBlank(CommandLine[i]))
				{
					++(cmd->TotalFileNumber);
					CurrentState = START_STATE;
				}
				else if (isQuote(CommandLine[i]))
				{
					PrintInfoAux("Invalid option format: ");
					PrintInfoEx(CommandLine + begin, i - begin + 1);
					PrintInfoAux("\n");
					return ERR_INVALID_COMMAND;
				}
				else
				{
					++(cmd->files[cmd->TotalFileNumber].length);
					CurrentState = FILE_STATE;
				}
			}
		}

		++i;
	}

	switch (CurrentState)
	{
	case START_STATE:
		TRACE("B");
		break;
	case SWITCH_STATE:
		PrintInfoAux("Invalid option format: ");
		PrintInfoEx(CommandLine + begin, i - begin + 1);
		PrintInfoAux("\n");
		return ERR_INVALID_COMMAND;
	case OPTION_STATE:
		TRACE("O");
		++(cmd->TotalOptionNumber);
		break;
	case SEPARATE_STATE:
		PrintInfoAux("Invalid option format: ");
		PrintInfoEx(CommandLine + begin, i - begin + 1);
		PrintInfoAux("\n");
		return ERR_INVALID_COMMAND;
	case PARAM_STATE:
		TRACE("P");
		++(cmd->TotalOptionNumber);
		break;
	case FILE_STATE:
		TRACE("F");
		++(cmd->TotalFileNumber);
	}

	TRACE("\n");

	return 0;
}
